import { manageCount, manageList } from "../../requster/api"

const state = {
    list: [],
    // 条数
    total: 0,
    // 页数
    size: 2,
    // 当前页数
    page: 1,

}

const mutations = {
    changeList(state, arr) {
        state.list = arr
    },
    changetotal(state, num) {
        state.total = num
    },
    changepage(state, num) {
        state.page = num
    }

}
const actions = {
    reqchangelist(context) {
        manageList({ size: context.state.size, page: context.state.page }).then(res => {
            let list = res.data.list ? res.data.list : []
            // bug解决
            if (list.length == 0 && context.state.page > 1) {
                context.commit("changepage", context.state.page - 1)
                context.dispatch("reqchangelist")
            }

            context.commit("changeList", list)
        })

    },
    reqchangepage(context, num) {
        context.commit("changepage", num)
        // 重新请求列表
        context.dispatch("reqchangelist")
    },
    reqchangetotal(context) {
        manageCount().then(res => {
            context.commit("changetotal", res.data.list[0].total)
        })
    },

}

const getters = {
    list(state) {
        return state.list
    },
    total(state) {
        return state.total
    },
    size(state) {
        return state.size
    }
}
export default {
    state,
    mutations,
    actions,
    getters,
    namespaced: true
}